<%--
  Copyright 1997-2009 Day Management AG
  Barfuesserplatz 6, 4001 Basel, Switzerland
  All Rights Reserved.

  This software is the confidential and proprietary information of
  Day Management AG, ("Confidential Information"). You shall not
  disclose such Confidential Information and shall use it only in
  accordance with the terms of the license agreement you entered into
  with Day.

  ==============================================================================

  Default head script.

  Draws the HTML head with some default content:
  - initialization of the WCM
  - includes the current design CSS
  - sets the HTML title

  ==============================================================================

--%><%@page session="false" import="com.day.cq.wcm.api.WCMMode"  %><%
%><%@include file="/libs/wcm/global.jsp" %><%
%><head>
    <meta http-equiv="content-type" content="text/html">
    <meta http-equiv="keywords" content="<%= WCMUtils.getKeywords(currentPage) %>">
    
    <cq:include script="/libs/wcm/core/components/init/init.jsp"/>
    
    <cq:includeClientLib categories="apps.geometrixx-main"/>
    <%
    currentDesign.writeCssIncludes(pageContext); 
    %>
    
    <title><%= currentPage.getTitle() == null ? currentPage.getName() : currentPage.getTitle() %></title>
    
    
    <% if (WCMMode.fromRequest(request) == WCMMode.EDIT){ %>
    <script type="text/javascript">
    CQ.Ext.ns('Ext.ux');

    /**
     * @class Ext.ux.JSONP
     * @author Tom DuPont
     * @version 2.0
     */
    Ext.ux.JSONP = {
        // Private Properties
        /**
         * @private
         * @property
         * @type Array
         */
        queue : [],
        /**
         * @private
         * @property
         * @type Ext.util.DelayedTask
         */
        timer : null,
        /**
         * @private
         * @property
         * @type Boolean
         */
        isActive : false,

        // Public
        /**
         * @public
         * @method
         * @param {Object} options
         */
        request : function(options) {
            if (!options || CQ.Ext.isEmpty(options.url)) {
                return;
            }
 
            var request = new Ext.ux.JsonpRequest(options);
            this.queue.push(request);

            this.processQueue();
        },
        /**
         * @public
         * @method
         * @param {Object} json
         */
        callback : function(json) {
            this.timer.cancel();
            this.isActive = false;

            var request = this.queue.shift();
            request.processResponse(json);
            CQ.Ext.destroy(request);

            this.processQueue();
        },

        // Private
        /**
         * @private
         * @method
         */
        processQueue : function() {
            if (this.isActive || this.queue.length == 0) {
                return;
            }

            var request = this.queue[0];
            request.begin();

            if (!this.timer) {
                this.timer = new CQ.Ext.util.DelayedTask(this.callback, this);
            }
            var timeout = request.timeout || 30000;
            this.timer.delay(timeout);
            this.isActive = true;
        }
    };

    /**
     * @class Ext.ux.JsonpRequest
     * @author Tom DuPont
     * @version 2.0
     * @extends Ext.util.Observable
     */
        Ext.ux.JsonpRequest = CQ.Ext.extend(CQ.Ext.util.Observable, {
            // Overrides
            /**
             * @constructor
             * @param {Object} options
             */
            constructor : function(options) {
                CQ.Ext.apply(this, options);
                this.params[this.callbackKey] = 'Ext.ux.JSONP.callback';
                this.script = document.createElement('script');
                this.script.type = 'text/javascript';

                if (options.isRawJSON) {
                    if (CQ.Ext.isIE) {
                        CQ.Ext.fly(this.script).on({
                                readystatechange : this.onReadyStateChange,
                                scope : this
                            });
                    } else {
                        CQ.Ext.fly(this.script).on({
                                load : this.onLoad,
                                scope : this
                            });
                    }
                }
            },

            // Properties
            /**
             * @public
             * @property
             * @config {String} url
             * @type String
             */
            url : null,
            /**
             * @public
             * @property
             * @config {Function} success
             * @type Function
             */
            success : null,
            /**
             * @public
             * @property
             * @config {Function} failure
             * @type Function
             */
            failure : null,
            /**
             * @public
             * @property
             * @config {Function} callback
             * @type Function
             */
            callback : null,
            /**
             * @public
             * @property
             * @config {Object} scope
             * @type Object
             */
            scope : null,
            /**
             * @public
             * @property
             * @config {Object} params
             * @type Object
             */
            params : {},
            /**
             * @public
             * @property
             * @config {String} callbackKey
             * @type String
             */
            callbackKey : 'jsoncallback',
            /**
             * @private
             * @property
             * @type HTMLElement
             */
            script : null,
            /**
             * @public
             * @property
             * @config {Number} timeout
             * @type Number
             */
            timeout : 10000,

            // Public Methods
            /**
             * @public
             * @method
             * @param {Object} json
             */
            processResponse : function(json) {
                if (this.callback) {
                    this.callback.apply(this.scope);
                }
                if (json) {
                    if (this.success) {
                        this.success.apply(this.scope, [json]);
                    }
                } else {
                    if (this.failure) {
                        this.failure.apply(this.scope);
                    }
                }
            },
            /**
             * @public
             * @method
             */
            begin : function() {
                var params = CQ.Ext.urlEncode(this.params);
                this.script.src = this.url + '?' + params;
                document.getElementsByTagName('head')[0].appendChild(this.script);
            },
            /**
             * @public
             * @method
             */
            destroy : function() {
                CQ.Ext.fly(this.script).removeAllListeners();
                document.getElementsByTagName('head')[0].removeChild(this.script);
            },

            // Handlers
            /**
             * @private
             * @method
             */
            onReadyStateChange : function() {
                if (this.script.readyState == 'complete') {
                    this.onLoad();
                }
            },
            /**
             * @private
             * @method
             */
            onLoad : function() {
                var json = this.script.innerHTML;
                var data = json.length ? CQ.Ext.decode(json) : null;

                Ext.ux.JSONP.callback(data);
            }
        });



    
    
        CQ.Ext.onReady(function() {
         IconCombo = CQ.Ext.extend(CQ.Ext.form.ComboBox, {
             
             
            initComponent:function() {
             var me = this;

             this.store = new CQ.Ext.data.ArrayStore({
                id: 0,
                fields: [
                    'link',
                    'media'
                ],
                root:'items' 
             });

             var store = this.store;

             CQ.Ext.apply({
                 store:me.store,
                 displayField:'link',
                 hiddenId:'link',
                 hiddenName:'link',
                 name:'link',
                 valueField:'link'
             });
             
             Ext.ux.JSONP.callback = function(x){
                 me.store.loadData(x);
             }
             
             Ext.ux.JSONP.request({
                   url : 'http://api.flickr.com/services/feeds/photos_public.gne',
                   params: {
                       format: 'json',
                      // tags: CQ.Ext.fly('search-value').dom.value,
                       tagmode: 'all',
                       lang: 'en-us'                            
                   },
                   
                   scope : this
                });
            
                // call parent initComponent
                IconCombo.superclass.initComponent.call(this);


                
            } // end of function initComponent

         
        });

         

        CQ.Ext.reg('abc',IconCombo);
        
        
    });
    </script>
    <% } %>
</head>
